package com.xzjt.controller;

import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xzjt.entity.SysRole;
import com.xzjt.entity.SysUser;
import com.xzjt.service.SysRoleService;
import com.xzjt.service.SysUserService;
import com.xzjt.utils.JwtUtil;
import com.xzjt.utils.R;
import com.xzjt.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName: SysUserController
 * @Author: yuandong
 * @Date 2021/10/11 13:13
 */
@Slf4j
@RestController
@RequestMapping("/user")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    @Autowired
    private SysRoleService roleService;

    /**
     * 分页查询
     * 如果前端没有传递参数，可以在这里使用defaultValue设置默认值
     *
     * @param pageNum
     * @param pageSize
     * @param sysUser
     * @return
     */
    @GetMapping("/selectList")
    public R<?> selectList(@RequestParam(defaultValue = "1") Integer pageNum,  @RequestParam(defaultValue = "10") Integer pageSize,  SysUser sysUser) {

        PageHelper.startPage(pageNum, pageSize);
        List<SysUser> userList = sysUserService.selectUserList(sysUser);
        PageInfo<SysUser> pageInfo =new PageInfo<>(userList);
        return R.ok(pageInfo);
    }


    /**
     * 查询用户详情
     * @param token
     * @return
     */
    @GetMapping("/info")
    public R userinfo(@RequestHeader  String token) {
        String username = JwtUtil.getUsername(token);
        SysUser sysUser = sysUserService.selectByUsername(username);
        //获取用户对应的角色
        List<SysRole> roleList = roleService.selectRoleAll();

        //如果是超管则查询所有角色
        List<SysRole> roles = sysUser.getIsadmin()==1 ? roleList : roleList.stream().filter(r -> !r.isAdmin()).collect(Collectors.toList());

        JSONObject ret=new JSONObject();
        ret.put("roles",roles);
        ret.put("user",sysUser);

        return R.ok(ret);
    }


    /**
     * 新增用户
     *
     * @return
     * @RequestBody 前台传递的json 转换成user对象
     */
    @PostMapping("/save")
    public R<?> save(@RequestBody SysUser user) {
        //验证用户名是否唯一
       int count=this.sysUserService.checkUserNameUnique(user.getUsername());
       if(count>0){
           return  R.error("-1" ,"失败，登录账号已存在");
       }

        if (user.getPassword() == null || user.getPassword().equals("")) {
            user.setPassword("123");
        }
        int flag = this.sysUserService.save(user);
        if(flag>0){
            return R.ok();
        }else{
            return R.error("-1","新增用户失败");
        }

    }


    /**
     * 修改用户
     *
     * @param user
     * @return
     */
    @PutMapping("/update")
    public R<?> update(@RequestBody SysUser user) {
        this.sysUserService.updateUserById(user);
        return R.ok();
    }





}
